<template>
  <div>
    <el-card>
      <el-breadcrumb separator="/" class="box-card">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-table
        :data="tableData"
        border
        style="width: 100%; margin-button: 10px"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row
              class="row-bg"
              justify="space-around"
              :class="['rowmenu', 'rowbottom', index == 0 ? 'rowtop' : '']"
              v-for="(item, index) in props.row.children"
              :key="index"
            >
              <el-col :span="5">
                <el-tag>
                  {{ item.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="['rowmenu', index2 == 0 ? '' : 'rowtop']"
                  v-for="(two, index2) in item.children"
                  :key="index2"
                >
                  <el-col :span="6">
                    <el-tag type="success">{{ two.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-row
                      v-for="(three, index3) in two.children"
                      :key="index3"
                    >
                      <el-tag
                        type="danger"
                        closable
                        @close="delrule(props.row, three.id)"
                        >{{ three.authName }}</el-tag
                      >
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作">
          <el-table-column fixed="right" label="操作" width="320">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="goeait(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="dek(scope.row)"
                >删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-s-tools"
                @click="editpower(scope.row)"
                >分配权限</el-button
              >
            </template>
            <template slot="header">
              <el-button type="primary" size="mini" @click="gosave()"
                >添加角色</el-button
              >
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

      <el-dialog
        title="分配权限"
        @close="clearkeys()"
        :visible.sync="powerShow"
        :close-on-click-modal="false"
      >
        <el-tree
          :data="treeData"
          :props="defaultProps"
          :default-checked-keys="defaultDataKey"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          highlight-current
        ></el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="powerShow = false">取消</el-button>
          <el-button type="primary" @click="setpower()">确定</el-button>
        </div>
      </el-dialog>
    </el-card>
    <el-dialog
      title="编辑角色"
      :visible.sync="powerShows"
      ref="saveform"
      :close-on-click-modal="false"
    >
      <el-form :model="tabledataobj">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="tabledataobj.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="tabledataobj.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="powerShows = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "roles",
  data() {
    return {
      tableData: [],
      powerShow: false,
      powerShows: false,
      treeData: [],
      defaultProps: {
        label: "authName",
        children: "children",
      },
      tabledataobj: {
        roleId: 0,
        roleName: "",
        roleDesc: "",
      },
      defaultDataKey: [],
      dialogVisible: false,
    };
  },
  methods: {
    async query() {
      let rst = await this.$http.get("roles");
      if (rst.meta.status == 200) {
        this.tableData = rst.data;
      } else {
        this.$message.error("获取结果失败");
      }
      console.log(rst);
    },

    delrule(row, rightId) {
      console.log(row);
      this.$confirm("此操作将永久删除该文件,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(
        async () => {
          let rst = await this.$http.delete(
            `roles/${row.id}/rights/${rightId}`
          );
          if (rst.meta.status == 200) {
            console.log(rst);
            row.children = rst.data;
          } else {
            this.$message.error("删除失败");
          }
        },
        () => {
          this.$message.info("取消删除");
        }
      );
    },
    async editpower(row) {
      let rst = await this.$http.get("rights/tree");
      console.log(rst);
      if (rst.meta.status == 200) {
        this.ruleid = row.id;
        this.powerShow = true;
        this.treeData = rst.data;
        this.getkeys(row, this.defaultDataKey);
        console.log(this.defaultDataKey);
      } else {
        this.$message.error("分配权限失败");
      }
    },
    getkeys(node, array) {
      if (!node.children) {
        array.push(node.id);
        return;
      }
      node.children.forEach((item) => {
        this.getkeys(item, array);
      });
    },
    clearkeys() {
      this.defaultDataKey = [];
    },
    async setpower() {
      let checkkey = this.$refs.tree.getCheckedKeys();
      let halfkey = this.$refs.tree.getHalfCheckedKeys();
      let keys = [...checkkey, ...halfkey].join();
      console.log(keys);
      let rst = await this.$http.post(`roles/${this.ruleid}/rights`, {
        rids: keys,
      });
      if (rst.meta.status == 200) {
        this.powerShow = false;
        this.query();
        this.$message.success("分配权限成功");
      } else {
        this.$message.error("分配权限失败");
      }
    },
    async goeait(row) {
      let rst = await this.$http.get(`roles/${row.id}`);
      console.log(rst);
      if (rst.meta.status == 200) {
        this.powerShows = true;
        this.tabledataobj.roleDesc = rst.data.roleDesc;
        this.tabledataobj.roleId = rst.data.roleId;
        this.tabledataobj.roleName = rst.data.roleName;
      } else {
        this.$message.error("获取数据失败");
      }
    },
    async save() {
      let rst = null;
      if (this.tabledataobj.roleId != 0) {
        rst = await this.$http.put(
          `roles/${this.tabledataobj.roleId}`,
          this.tabledataobj
        );
      } else {
        rst = await this.$http.post("roles", this.tabledataobj);
      }
      if (rst.meta.status == 201 || rst.meta.status == 200) {
        this.$message.success("保存成功");
        this.powerShows = false;
        this.query();
      } else {
        this.$message.error("添加失败");
      }
    },
    gosave() {
      this.powerShows = true;
      this.tabledataobj = {
        ruleid: 0,
        roleDesc: "",
        roleName: "",
      };
    },

    dek(obj) {
      this.$confirm(`此操作将永久删除该文件，是否继续？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let rst = await this.$http.delete(`roles/${obj.id}`);
          if (rst.meta.status == 200) {
            this.query();
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
  mounted() {
    this.query();
  },
};
</script>

<style scoped>
.box-card {
  padding-bottom: 10px;
}
.el-tag {
  margin: 7px;
}
/* .rowtop {
  border-top: 1px solid #F6F7F8;
}
.rowbottom {
  border-bottom: 1px solid #F6F7F8;
} */
.rowmenu {
  display: flex;
  align-items: center;
}
</style>